package org.wanda.pv.controller.hy;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wanda.pv.framework.base.Result;
import org.wanda.pv.model.hy.HyDrawing;
import org.wanda.pv.model.hy.HyProjectInfo;
import org.wanda.pv.model.hy.HyTree;
import org.wanda.pv.service.hy.HyDrawingService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lxy
 * Created by 2018/11/22 14:37
 */
@RestController
@RequestMapping("/hy/drawing")
public class HyDrawingController {
    @Autowired
    Dao dao;
    @Autowired
    private HyDrawingService hyDrawingService;

    private final static Logger log = LoggerFactory.getLogger(HyDrawingController.class);


    /**
     *  旋转图纸的图片角度
     * @param drawingId  图纸id
     * @param angel 旋转角度
     */
    @RequestMapping("/rotatePdfImage")
    public Object rotatePdfImage(Long drawingId ,int angel){
        return hyDrawingService.rotatePdfImage(drawingId,angel);
    }

    /**
     * 获取建筑节点下的第一个图纸状态
     * @param request
     * @return
     */
    @RequestMapping("/getFirstDrawing")
    public Object getFirstDrawing(HttpServletRequest request){
        List<HyDrawing> drawings=null;
        HyDrawing hyDrawing=null;
        try {
            HyTree hyTree=dao.fetch(HyTree.class,Cnd.where("name","=","01-AR建筑"));
            String flowMapModelId= hyTree.getId();
            String projectFlowChildrenNodeId= (String) request.getSession().getAttribute("projectFlowChildrenNodeId");
            if(flowMapModelId.isEmpty() || projectFlowChildrenNodeId.isEmpty()){
                return Result.error("Session中节点ID为空,请重新登录！");
            }else{
                drawings=dao.query(HyDrawing.class, Cnd.where("flow_map_model_id","=",flowMapModelId).and("project_flow_children_node_id","=",projectFlowChildrenNodeId).and("latest","=",true).and("delFlag","=","0").limit(1));

                if (drawings.size() > 0){
                    hyDrawing= drawings.get(0);
                    dao.fetchLinks(hyDrawing,null);
                } else {
                    return Result.error("当前节点下没有图纸文件！");
                }
            }
        } catch (Exception e){
            log.error(e.getMessage());
        }
        return Result.success("success",hyDrawing);
    }

    /**
     * 获取指定节点下所有的图纸文件
     * @param flowMapModelId
     * @param projectFlowChildrenNodeId
     * @param projectId
     * @return
     */
    @RequestMapping("/getDrawing")
    public Object getDrawing(String flowMapModelId,String projectFlowChildrenNodeId,String projectId){
        Map<String,Object> map=new HashMap<>();
        List<HyDrawing> drawings=null;
        try {
            if(flowMapModelId.isEmpty() || projectFlowChildrenNodeId.isEmpty()){
                return Result.error("节点ID不能为空！");
            }else{
                drawings=dao.query(HyDrawing.class, Cnd.where("flow_map_model_id","=",flowMapModelId).and("project_flow_children_node_id","=",projectFlowChildrenNodeId).and("latest","=",true).and("delFlag","=","0").orderBy("location","asc"));
                drawings.forEach(drawing->{
                    dao.fetchLinks(drawing,null);
                });
                //  查询当前节点下子节点的图纸文件
                List<HyTree> hyTreeList = dao.query(HyTree.class, Cnd.where("parent_id", "=", flowMapModelId));
                for (HyTree hyTree : hyTreeList ) {
                    List<HyDrawing> drawingList=  dao.query(HyDrawing.class, Cnd.where("flow_map_model_id","=",hyTree.getId()).and("project_flow_children_node_id","=",projectFlowChildrenNodeId).and("latest","=",true).and("delFlag","=","0").orderBy("location","asc"));
                    for (HyDrawing hyDrawing : drawingList) {
                        dao.fetchLinks(hyDrawing,null);
                        hyDrawing.setFileName(hyDrawing.getFileName() + "(" +hyTree.getName()+ "子节点下图纸文件）" );
                        drawings.add(hyDrawing);
                    }
                }

                HyProjectInfo hyProjectInfo=dao.fetch(HyProjectInfo.class,Cnd.where("id","=",projectId));
                map.put("drawings",drawings);
                map.put("projectInfo",hyProjectInfo);
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return Result.success("success",map);
    }

    /**
     * 获取项目下所有的图纸文件信息
     * @param request
     * @return
     */
    @RequestMapping("/getAllDrawings")
    public Object getAllDrawings(HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        List<HyDrawing> drawings=null;
        try {
            String projectId= (String) request.getSession().getAttribute("projectId");
            if(projectId.isEmpty()){
                return Result.error("Session中项目id为空，请重新登录");
            }else{
                drawings=dao.query(HyDrawing.class, Cnd.where("hy_project_info_id","=",projectId).and("latest","=","true").and("delFlag","=","0").orderBy("location","asc"));
                drawings.forEach(drawing->{
                    dao.fetchLinks(drawing,null);
                });
                HyProjectInfo hyProjectInfo=dao.fetch(HyProjectInfo.class,Cnd.where("id","=",projectId));

                map.put("drawings",drawings);
                map.put("projectInfo",hyProjectInfo);
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return Result.success("success",map);
    }

    /**
     * 根据orgId获取图纸信息
     * @param orgId
     * @return
     */
    @RequestMapping("/getDrawingById")
    public Object getDrawingByOrgId(String orgId){
        HyDrawing drawings=null;
        try {
            if(orgId.isEmpty()){
                return Result.error("OrgID不能为空！");
            }else{
                drawings=dao.fetch(HyDrawing.class, Cnd.where("org_id","=",orgId).and("latest","=",true).and("delFlag","=","0"));
                dao.fetchLinks(drawings,null);
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return Result.success("success",drawings);
    }


}
